﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using FitnessDiary.WebUI.Models;
using FitnessDiary.Domain.Entities;
using System.Security.Policy;

namespace FitnessDiary.WebUI.ViewHelpers
{
	public static class DiaryHelper
	{
		public static MvcHtmlString ScrollWeeks(this HtmlHelper html, int week, Func<string, int, string> url)
		{
			TagBuilder prevTag = CreateWeekLink(week - 1, url, false);
			TagBuilder nextTag = CreateWeekLink(week + 1, url, true);

			StringBuilder result = new StringBuilder();
			result.Append(prevTag.ToString());
			result.Append(nextTag.ToString());
			return MvcHtmlString.Create(result.ToString());
		}

		private static TagBuilder CreateWeekLink(int week, Func<string, int, string> url, bool nextWeek)
		{
			TagBuilder tag = new TagBuilder("a");
			if (week < 0)
				tag.MergeAttribute("href", url("Prev", -week));
			else if (week > 0)
				tag.MergeAttribute("href", url("Next", week));
			else
				tag.MergeAttribute("href", url("Index", 0));

			tag.InnerHtml = nextWeek ? "Next&gt&gt" : "&lt&ltPrev";

			return tag;
		}

		public static MvcHtmlString CreateDay(this HtmlHelper html, Day day, DateTime today, string actionLink)
		{
			StringBuilder result = new StringBuilder();

			TagBuilder calendarDayDiv = CreateCalendarDayDiv(day.Date, today);
			calendarDayDiv.MergeAttribute("class", "calendar-day");

			TagBuilder calendarDayDateDiv = new TagBuilder("div");
			calendarDayDateDiv.MergeAttribute("class", "calendar-day-date");

			TagBuilder calendarDayHitsDiv = new TagBuilder("div");
			calendarDayHitsDiv.MergeAttribute("class", "calendar-day-hits");

			calendarDayDateDiv.InnerHtml = actionLink;

			List<TagBuilder> links = CreateLinks(day);
			foreach (TagBuilder link in links)
			{
				calendarDayHitsDiv.InnerHtml += link.ToString();
			}
			if (links.Count > 0)
			{
				calendarDayDiv.MergeAttribute("has-hit", "true");
			}

			calendarDayDiv.InnerHtml = calendarDayDateDiv.ToString() + calendarDayHitsDiv.ToString();
			result.Append(calendarDayDiv);
			return MvcHtmlString.Create(result.ToString());
		}

		public static List<TagBuilder> CreateLinks(Day day)
		{
			List<TagBuilder> links = new List<TagBuilder>();
			Dictionary<int, int> statIds = CreateStatIds(day);
			foreach (Hit hit in day.Hits)
			{
				TagBuilder tag = new TagBuilder("a");
				if (statIds[hit.Stat.Id] == 1)
				{
					tag.MergeAttribute("href", "/Hit?id=" + hit.Id);
					tag.InnerHtml = hit.Stat.Name;
					links.Add(tag);
				}
				else if (statIds[hit.Stat.Id] > 1)
				{
					tag.MergeAttribute("href", "/Hit/StatOnDay?stat=" + hit.Stat.Id + "&date=" + hit.Date.ToString(DateFormat()));
					tag.InnerHtml = hit.Stat.Name + " x" + statIds[hit.Stat.Id];
					links.Add(tag);
					statIds[hit.Stat.Id] = 0;
				}
			}
			return links;
		}

		public static Dictionary<int, int> CreateStatIds(Day day)
		{
			Dictionary<int, int> statIds = new Dictionary<int, int>();
			foreach (Hit hit in day.Hits)
			{
				if (!statIds.ContainsKey(hit.Stat.Id))
				{
					statIds.Add(hit.Stat.Id, 1);
				}
				else
				{
					statIds[hit.Stat.Id]++;
				}
			}
			return statIds;
		}

		public static TagBuilder CreateCalendarDayDiv(DateTime date, DateTime today)
		{
			TagBuilder calendarDayDiv = new TagBuilder("div");
			if (DateTime.Compare(date, today) < 0)
			{
				calendarDayDiv.MergeAttribute("day-type", "past");
			}
			else if (DateTime.Compare(date, today) == 0)
			{
				calendarDayDiv.MergeAttribute("day-type", "present");
			}
			else
			{
				calendarDayDiv.MergeAttribute("day-type", "future");
			}
			return calendarDayDiv;
		}

		public static string DateFormat()
		{
			return "yyyy-MM-dd";
		}
	}
}